<!-- 新建模板 -->
<!-- 编辑器 -->
<template>
  <div class="editor-page">
    <LeftsideMenu @getxz="getxz" ref="leftSideRef"></LeftsideMenu>
    <p class="tr z9 pd pt5 dsfsdfsde">
      温馨提示：
      1.双击分区空白处设置分区高度<br />
      2.点击控件编辑<br />
      3.选中控件按delete键删除控件
    </p>

    <div v-for="(item,idx) in PartitionData" :key="idx">
      <titleControl @getObj="getObj" titletext="标题栏" :initH="item.initH" @click="setH(item)" v-if="item.type==0"
        :ref="el => titleControlaRef = el " :initData="item.initData"></titleControl>
      <titleControl @getObj="getObj" titletext="页头栏" :initH="item.initH" v-if="item.type==1" @click="setH(item)"
        :ref="el => titleControlbRef = el " :initData="item.initData">
      </titleControl>
      <titleControl @getObj="getObj" titletext="正文栏" :initH="item.initH" v-if="item.type==2" @click="setH(item)"
        :ref="el => titleControlcRef = el " :initData="item.initData">
      </titleControl>
      <titleControl @getObj="getObj" titletext="页尾栏" :initH="item.initH" v-if="item.type==3" @click="setH(item)"
        :ref="el => titleControldRef = el " :initData="item.initData" :initLeft="true">
      </titleControl>
      <titleControl @getObj="getObj" titletext="总结栏" :initH="item.initH" v-if="item.type==4" @click="setH(item)"
        :ref="el => titleControleRef = el " :initData="item.initData" :initLeft="true">
      </titleControl>
    </div>
    <RightsideMenu :obj="obj"></RightsideMenu>
    <div class="manipulation-menu">
      <div class="manipulation-menu-context tr">
        <n-button size="medium" @click="callBack">返回</n-button>
        <n-button type="primary" size="medium" class="ml20" @click="showModal=true">保存模板</n-button>
      </div>
    </div>
    <n-modal v-model:show="showModal" preset="card" class="Stock-diagnosis">
      <template #header>
        <div>{{isbj?'保存':'添加'}}模板</div>
      </template>
      <div>
        <n-form ref="formRef" :label-width="80">
          <n-form-item label="模板名称">
            <n-input placeholder="输入模板名称" v-model:value='fromData.name' />
          </n-form-item>

          <n-form-item label="模板类型">
            <n-select v-model:value="fromData.templateTtpe" :options="templateList" />
          </n-form-item>
        </n-form>
      </div>
      <template #action>
        <div class="tr">
          <n-button @click="showModal = false"> 取消 </n-button>
          <n-button type="success" class="ml20" @click="browse"> 确认 </n-button>
        </div>
      </template>
    </n-modal>
  </div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import { dxpost ,callBack,qurl, dxget} from "../../util/index";
import LeftsideMenu from "../public/components/LeftsideMenu.vue";
import RightsideMenu from "../public/components/RightsideMenu.vue";
import titleControl from "../public/components/titleControl.vue";
const cz  = qurl()
const showModal = ref(false)
const isbj = ref(false)
const nextLa = ref(true) // 状态
const leftSideRef = ref()
const titleControlaRef = ref()
const titleControlbRef = ref()
const titleControlcRef = ref()
const titleControldRef = ref()
const titleControleRef = ref()
const PartitionData = ref([])
const formRef = ref()
const fromData = ref({
  name: "",
  templateTtpe: 1,
  partition: null
})

const obj = ref(); // 这个传到右侧的菜单
const templateList = ref([
  {
    label: '表格',
    value: 1
  },
  {
    label: '表单',
    value: 2
  }
])
const getObj = (data) => {
  obj.value = data
}
const getxz = (data) => {
  if (nextLa.value) {
    return
  }
  PartitionData.value = data
}
const setH = (data) => {
  obj.value = data
}


const browse = async () => {
  if (!fromData.value.name) {
    window.$message.error("请输入模板名称")
    return
  }
  const partition = PartitionData.value
  if (titleControlaRef.value) {
    partition.filter(a => a.type == 0)[0].initData = titleControlaRef.value.getDatas()
  }
  if (titleControlbRef.value) {
    partition.filter(a => a.type == 1)[0].initData = titleControlbRef.value.getDatas()
  }
  if (titleControlcRef.value) {
    partition.filter(a => a.type == 2)[0].initData = titleControlcRef.value.getDatas()
  }
  if (titleControldRef.value) {
    partition.filter(a => a.type == 3)[0].initData = titleControldRef.value.getDatas()
  }
  if (titleControleRef.value) {
    partition.filter(a => a.type == 4)[0].initData = titleControleRef.value.getDatas()
  }

  fromData.value.partition = JSON.stringify(partition)
  let jhbber = "post"
  if(isbj.value){
    jhbber = "put"
  }
  const { errno, data }:any = await dxpost('huli/hltemplateList', fromData.value,jhbber)

  if (errno != 0) {
    window.$message.error('添加失败')
  }
  window.$message.success('操作成功')
  callBack()

}
const initDta = async() => {
  const {data}:any = await dxget("huli/hltemplateList",{id:cz.id})
  console.log(data);
  fromData.value = data
  const partition = JSON.parse(fromData.value.partition)
  leftSideRef.value.setData(partition)
  PartitionData.value = partition
  nextLa.value = false
}


if(cz.id){
  isbj.value = true
  initDta()
}else{
  nextLa.value = false
}

</script>
<style scoped>
.dsfsdfsde {
  position: absolute;
  right: 0;
}

.editor-page {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: #fff;
  z-index: 9999;
  overflow: auto;
}

.editor-bj {
  width: 900px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  margin: auto;
  overflow: auto;
  position: relative;
  left: -50px;
}

.title {
  text-align: center;
  color: #42b983;
}

.wrapper {
  display: flex;
  justify-content: center;
  width: 100%;
}

.item {
  width: 100px;
  height: 100px;
  font-size: 50px;
  text-align: center;
  line-height: 100px;
  margin: 10px;
  background-color: #42b983;
  color: #ffffff;
}

.manipulation-menu {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 60px;
  background: #fff;
  box-shadow: 0 -5px 5px rgba(0, 0, 0, 0.1);
  z-index: 999;
}

.manipulation-menu-context {
  width: 1200px;
  margin: auto;
  line-height: 60px;
}
</style>
